﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web.Sort
{
    public partial class Sort : System.Web.UI.Page
    {
        public int[] arr = { 10, 43, 55, 12, 20, 9, 8, 34, 77, 55, 20, 90 };
        protected void Page_Load(object sender, EventArgs e)
        {
            string type = "H";
            switch (type)
            {
                case "A":
                    A();
                    break;
                case "B":
                    B();
                    break;
                case "C":
                    C();
                    break;
                case "D":
                    D();
                    break;
                case "E":
                    E();
                    break;
                case "F":
                    F();
                    break;
                case "H":
                    H();
                    break;
                case "G":
                    G();
                    break;
                default: break;
            }
        }
        //BubbleSort
        private void A()
        {
            MSL.Utility.Sort.BubbleSort sort = new MSL.Utility.Sort.BubbleSort();
            sort.Desc<int>(arr);
            sort.Asc<int>(arr);

            sort.CocktailDesc<int>(arr);
            sort.CocktailAsc<int>(arr);
        }
        //InsertionSort
        private void B()
        {
            MSL.Utility.Sort.InsertSort sort = new MSL.Utility.Sort.InsertSort();
            sort.Desc<int>(arr);
            sort.Asc<int>(arr);
        }
        //MergeSort 不通过
        private void C()
        {
            MSL.Utility.Sort.MergeSort sort = new MSL.Utility.Sort.MergeSort();
            sort.Desc<int>(arr);
            sort.Asc<int>(arr);
        }
        //QuickSort
        private void D()
        {
            MSL.Utility.Sort.QuickSort sort = new MSL.Utility.Sort.QuickSort();
            sort.Desc<int>(arr);
            sort.Asc<int>(arr);
        }
        //SelectionSort
        private void E()
        {
            MSL.Utility.Sort.SelectSort sort = new MSL.Utility.Sort.SelectSort();
            sort.Desc<int>(arr);
            sort.Asc<int>(arr);
        }
        //ShellSort
        protected void F()
        {
            MSL.Utility.Sort.ShellSort sort = new MSL.Utility.Sort.ShellSort();
            sort.Desc<int>(arr);
            sort.Asc<int>(arr);
        }
        //ShellSort
        protected void G()
        {
            MSL.Utility.Sort.HeapSort sort = new MSL.Utility.Sort.HeapSort();
            sort.Desc<int>(arr);
            sort.Asc<int>(arr);
        }
        //排序效率整体测试
        protected void H()
        {
            SortTest(50);
            SortTest(500);
            SortTest(5000);
            SortTest(20000);
            //SortTest(30000);
        }
        private void SortTest(int n)
        {
            msg = "";
            int[] aa = RandomSet(n, 50000);
            RunTheMethod("SelectionSort", (Action<IList<int>>)(new MSL.Utility.Sort.SelectSort().Desc<int>), aa.Clone() as int[]);
            RunTheMethod("BubbleSort", (Action<IList<int>>)(new MSL.Utility.Sort.BubbleSort().Desc<int>), aa.Clone() as int[]);
            RunTheMethod("Cocktail", (Action<IList<int>>)(new MSL.Utility.Sort.BubbleSort().CocktailDesc<int>), aa.Clone() as int[]);
            RunTheMethod("InsertionSort", (Action<IList<int>>)(new MSL.Utility.Sort.InsertSort().Desc<int>), aa.Clone() as int[]);
            RunTheMethod("MergeSort", (Action<IList<int>>)(new MSL.Utility.Sort.MergeSort().Desc<int>), aa.Clone() as int[]);
            RunTheMethod("HeapSort", (Action<IList<int>>)(new MSL.Utility.Sort.HeapSort().Desc<int>), aa.Clone() as int[]);
            RunTheMethod("QuickSort", (Action<IList<int>>)(new MSL.Utility.Sort.QuickSort().Desc<int>), aa.Clone() as int[]);
            RunTheMethod("ShellSort", (Action<IList<int>>)(new MSL.Utility.Sort.ShellSort().Desc<int>), aa.Clone() as int[]);
            TestMicrosoft(aa.Clone() as int[]);

            Response.Write("<br/>数组长度为" + n + ",降序时间：<br/>" + msg);
            //msg = "";
            //msg += "QuickSort:";
            //RunTheMethod((Action<IList<int>>)(new MSL.Utility.Sort.QuickSort().Asc<int>), aa.Clone() as int[]);
            //msg += "SelectionSort:";
            //RunTheMethod((Action<IList<int>>)(new MSL.Utility.Sort.SelectionSort().Asc<int>), aa.Clone() as int[]);
            //msg += "BubbleSort:";
            //RunTheMethod((Action<IList<int>>)(new MSL.Utility.Sort.BubbleSort().Asc<int>), aa.Clone() as int[]);
            //msg += "HeapSort:";
            //RunTheMethod((Action<IList<int>>)(new MSL.Utility.Sort.HeapSort().Asc<int>), aa.Clone() as int[]);
            //msg += "InsertionSort:";
            //RunTheMethod((Action<IList<int>>)(new MSL.Utility.Sort.InsertionSort().Asc<int>), aa.Clone() as int[]);
            //msg += "MergeSort:";
            //RunTheMethod((Action<IList<int>>)(new MSL.Utility.Sort.MergeSort().Asc<int>), aa.Clone() as int[]);
            //msg += "ShellSort:";
            //RunTheMethod((Action<IList<int>>)(new MSL.Utility.Sort.ShellSort().Asc<int>), aa.Clone() as int[]);
            //msg += "TestMicrosoft:";
            //TestMicrosoft(aa.Clone() as int[]);

            //Response.Write("升序时间：" + msg);
        }
        public string msg = "";
        public int[] RandomSet(int length, int max)
        {
            int[] result = new int[length];
            Random rand = new Random();
            for (int i = 0; i < result.Length; i++)
            {
                result[i] = rand.Next(max);
            }
            return result;
        }
        public void RunTheMethod(string className, Action<IList<int>> method, IList<int> list)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            method(list);
            stopwatch.Stop();
            //string methodName = method.Method.Name;
            //int length = methodName.Length;
            string str = "";
            for (int i = 0; i < 50 - className.Length; i++)
            {
                str += "-";
            }
            //msg += methodName + "Time:" + stopwatch.Elapsed.TotalSeconds + "\n";
            msg += className + "-" + str + "Time:" + stopwatch.Elapsed.TotalSeconds + "<br/>";
        }
        public void TestMicrosoft(IList<int> list)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            List<int> result = list.OrderBy(a => a).ToList();
            stopwatch.Stop();
            string methodName = "TestMicrosoft";
            int length = methodName.Length;
            for (int i = 0; i < 50 - methodName.Length; i++)
            {
                methodName += "-";
            }
            msg += methodName + "-------------------" + "Time:" + stopwatch.Elapsed.TotalSeconds + "<br/>";
        }
    }
}